<?php
namespace Mobile\Controller;
use Think\Controller;


class WxController extends Controller {

   //  public function __construct() {
	 	// D('Stat','weixin')->saveStat('Haofangdong',"click");
//	 	//测试
//	    if($_GET['test'] ){
//	        $_SESSION['fbh_openid'] = "ojocnvw15xWKDxuChL6djnjRV36E";
//	    }
//		$this->fans = D('FansQA','FansQA');
//		//获取当前地址
//		$callbackurl = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
//		if(!$_SESSION['fbh_openid']){
//			$_SESSION['fbh_openid'] = Oauth2::getMyopenid($callbackurl,$this->AppId,$this->AppSecret);
//		}
//		$openid = $_SESSION['fbh_openid'];
//		if($openid){
//			$data = M()->table('hfd_card')->where('openid = "'.$openid.'"')->find();
//			if(!$data){
//				//获取用户信息
//				$info = Oauth2::get_userinfo($openid,$this->uid,$callbackurl);//获取用户信息
//				$_SESSION['info']     = $info;
//				//判断是否关注本平台
//				if($info['subscribe']===0){
//					//引导关注地址
//					if(!$info['fbh_openid']){
//						$code = $_REQUEST["code"];
//						if($code){//2.获取网页授权，后可以获取openid
//							$info = $this->get_old_userinfo_oauth2($code);
//							//file_put_contents ( "1.txt", json_encode ( $this->obj ) ,FILE_APPEND);
//							//$this->key->addInfo( $info ,'1');
//						}else{//1.获取code
//							$this->getUserCode_oauth2();
//							exit();
//						}
//					}
//				}
//				$this->addInfo($info,$data);
//				/*print_r($data);*/
//			}
//		}
 	// }	
//  /**
//	 * @param 获得用户基本信息
//	 */
//	public function get_old_userinfo_oauth2($code){
//
//		$arr          = $this->setACCESS_TOKEN_oauth2($code);
//
//		$access_token = $arr['access_token'];
//		$openid       = $arr['openid'];
//		$url          = 'https://api.weixin.qq.com/sns/userinfo?access_token=' . $access_token . '&openid=' . $openid . '&lang=zh_CN';
//		$jsonUser     = $this->getHttpResponseGET($url);//通过access_token和openid拉取用户基本信息
//		$objUser      = json_decode($jsonUser);
//	
//		return get_object_vars($objUser);//返回用户信息
//		
//	}
//	public function getUserCode_oauth2() {
//		$callbackurl = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];	
//		$authorize_url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=$this->AppId&redirect_uri=" . urlencode($callbackurl) . "&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect";
//		Header("Location:" . $authorize_url);
//	}
//	public function setACCESS_TOKEN_oauth2($code){
//		$AppId = $this->AppId;
//		$AppSecret = $this->AppSecret;
//		//https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET
//		$url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$AppId."&secret=".$AppSecret."&code=".$code."&grant_type=authorization_code";
//		//var_dump($url);exit();
//		$info = $this->getHttpResponseGET($url);
//		$arr = json_decode($info,$assoc=true);//通过AppId和AppSecret可以获取access_token
//		//file_put_contents("t.txt",$info);
//
//		return $arr;
//	}
//	public function getHttpResponseGET($url) {
//		$curl = curl_init($url);
//		curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);//SSL证书认证
//		curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);//不认证
//		curl_setopt($curl, CURLOPT_HEADER, 0 ); // 过滤HTTP头
//		curl_setopt($curl,CURLOPT_RETURNTRANSFER, 1);// 显示输出结果
//		$responseText = curl_exec($curl);
//		curl_close($curl);
//		return $responseText;
//	}
//	public function addInfo($info){
//		$data['nickname']   = $info['nickname'];
//		$data['headimgurl'] = $info['headimgurl'];
//		$data['openid']     = $info['openid'];
//		$data['ctime']      = time(); 
//		$id = M()->table('hfd_card')->add($data);
//	}

	//index.htmlҳ����ʷ���
    public function index(){

	$this->display();

    }		
    public function yaoqing(){
    	// $mobile='15040628405';
    	// $mobile2=15040628405;
    	// $row=M()->table('hfd_card')->where('mobile="'.$mobile.'"')->count();
    	// $row2=M()->table('hfd_card')->where('mobile="'.$mobile2.'"')->count();
    	// $this->assign('row',$row);
    	// $this->assign('row2',$row2);
    	$this->display();
    }
    
    	public function sendmassage(){
	    	$mobile = $_POST['mobile'];
	    	$code = $this->code();
	    	$target = "http://106.ihuyi.cn/webservice/sms.php?method=Submit";
	    	$post_data = "account=cf_fbh&password=9393e424c57b23295513d55beb31822d&mobile=".$mobile."&content=".rawurlencode("您的验证码是：".$code."。请不要把验证码泄露给其他人。");
	    	$gets =  $this->xml_to_array($this->Post($post_data, $target));
			if($gets['SubmitResult']['code']==2){
			    $_SESSION['mobile'] = $mobile;
			    $_SESSION['code'] = $code;
			}
			//$data['status'] = "1";
		$this->ajaxReturn("1",'JSON');
	}
	function code($length = 6 , $numeric = 0) {
	    PHP_VERSION < '4.2.0' && mt_srand((double)microtime() * 1000000);
	    if($numeric) {
	        $hash = sprintf('%0'.$length.'d', mt_rand(0, pow(10, $length) - 1));
	    } else {
	        $hash = '';
	        $chars = '01234567899876543210';
	        $max = strlen($chars) - 1;
	        for($i = 0; $i < $length; $i++) {
	            $hash .= $chars[mt_rand(0, $max)];
	        }
	    }
	    return $hash;
	}

	public function yz(){
		$mobile=$_POST['mobile'];
		$code=$_POST['code'];
		$datas['mobile']=$_POST['mobile'];
		$datas['card']="100元";
		$datas['status']="1";
		$datas['ctime']=time();
		$ishave=M()->table('hfd_card')->where('mobile= "'.$mobile.'"')->count();
		if($ishave != 0){
			$data['status'] = "1";
			$this->ajaxReturn('1','JSON');
		}else{
			if($_SESSION['code']==$code&&$_SESSION['mobile']=$mobile){
				$ctime=time();
				//$insert=M()->query("INSERT INTO `hfd_card` (`mobile`,`card`,`status`,`ctime`) VALUES ('".$mobile."','100元','1','".$ctime."')");
				$insert=M('hfd_card')->data($datas)->add();
				if($insert!=''){
					//$data['status'] = "0";
					$this->ajaxReturn("0","JSON");
				}else{
					//$data['status'] = "2";
					$this->ajaxReturn("2",'JSON');
				}
			}else{
				//$data['status'] = "3";
				$this->ajaxReturn("3",'JSON');
			}
		}
	}

	function Post($curlPost,$url){
	        $curl = curl_init();
	        curl_setopt($curl, CURLOPT_URL, $url);
	        curl_setopt($curl, CURLOPT_HEADER, false);
	        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
	        curl_setopt($curl, CURLOPT_NOBODY, true);
	        curl_setopt($curl, CURLOPT_POST, true);
	        curl_setopt($curl, CURLOPT_POSTFIELDS, $curlPost);
	        $return_str = curl_exec($curl);
	        curl_close($curl);
	        return $return_str;
	}
 
//将 xml数据转换为数组格式。
	function xml_to_array($xml){
	    $reg = "/<(\w+)[^>]*>([\\x00-\\xFF]*)<\\/\\1>/";
	    if(preg_match_all($reg, $xml, $matches)){
	        $count = count($matches[0]);
	        for($i = 0; $i < $count; $i++){
	        $subxml= $matches[2][$i];
	        $key = $matches[1][$i];
	            if(preg_match( $reg, $subxml )){
	                $arr[$key] = $this->xml_to_array( $subxml );
	            }else{
	                $arr[$key] = $subxml;
	            }
	        }
	    }
	    return $arr;
	}
}
?>